<?php


namespace api\crm\controller;


use api\crm\model\CompanyModel;
use api\crm\model\UserModel;
use app\admin\model\ConnectWayModel;
use app\admin\model\CustomerSourceSettingModel;
use app\admin\model\CustomerStatusModel;
use app\admin\model\IntentionTagModel;
use app\admin\model\LostReasonModel;
use app\admin\model\OtherModel;
use app\user\model\AssetModel;
use cmf\controller\RestBaseController;
use cmf\lib\Imgcompress;
use think\File;
use function GuzzleHttp\Promise\all;

class OtherController extends RestBaseController
{
    public function get_customer_source()
    {
        $user_id = $this->getUserId();
        $user_info = UserModel::get($user_id);
        $list = CustomerSourceSettingModel::where('company_id', $user_info['company_id'])->all();
        $way_list = ConnectWayModel::where(['company_id' => $user_info['company_id']])
            ->order('sort asc')->all();
        $company_info = CompanyModel::get($user_info['company_id']);
        $city = $company_info['region'] ? json_decode($company_info['region']) : '';
        $reason_list = LostReasonModel::where('company_id', $user_info['company_id'])
            ->order('sort asc')->all()->each(function ($item) {
                if ($item['options']) {
                    $item['options'] = json_decode($item['options']);
                }
                return $item;
            });
        $this->success('获取客户来源', ['list' => $list, 'way_list' => $way_list, 'city' => $city, 'reason_list' => $reason_list,'user_info'=>$company_info]);
    }

    public function get_customer_status()
    {
        $user_id = $this->getUserId();
        $user_info = UserModel::get($user_id);
        $data = $this->request->param();
        //传入当前用户id
        $id = isset($data['is_finished']) && $data['is_finished'] ? $data['is_finished'] : 0;
        $where[] = ['type', '=', $id];
        $list = CustomerStatusModel::with('tag_list')
            ->where('company_id', $user_info['company_id'])
            ->where($where)
            ->where('is_delete', 0)
            ->order('sort asc')
            ->all();
        $reason_list = LostReasonModel::where('company_id', $user_info['company_id'])
            ->order('sort asc')->all()->each(function ($item) {
                if ($item['options']) {
                    $item['options'] = json_decode($item['options']);
                }
                return $item;
            });
        $connect_way = ConnectWayModel::where('company_id', $user_info['company_id'])
            ->order('sort asc')->all();
//        $this->success('获取客户状态', $list);
        $this->success('获取客户状态', ['list' => $list, 'reason_list' => $reason_list, 'connect_way' => $connect_way]);
    }

    public function get_tag_list()
    {
        $user_id = $this->getUserId();
        $user_info = UserModel::get($user_id);
        $list = IntentionTagModel::where(['company_id' => $user_info['company_id'], 'type' => 1])
            ->all();
        $this->success('获取客户状态', $list);
    }

    //设计师页面 搜索 业务经理 和客户经理
    public function get_manager_list()
    {
        $user_id = $this->getUserId();
        $user_info = UserModel::get($user_id);
        $where[] = ['company_id', '=', $user_info['company_id']];
        $where[] = ['store_id', '=', $user_info['store_id']];
        $where[] = ['is_delete', '=', 0];
        if ($user_info['role_name'] == '店长') {
            $where[] = ['role_name', 'in', ['客户经理', '业务经理']];
        } else if ($user_info['role_name'] == '业务经理') {
            $where[] = ['role_name', 'in', ['客户经理']];
            $where[] = ['parent_id', '=', $user_id];
        } else if ($user_info['role_name'] == '客户经理') {
            $where[] = ['id', '=', $user_id];
        }
        $user_list = UserModel::field('id,true_name,role_name')
            ->order('create_time desc')->where($where)->all()->each(function ($item) {
                $item['name'] = $item['true_name'] . '(' . $item['role_name'] . ')';
                return $item;
            })->toArray();
        if ($user_info['role_name'] == '业务经理') {
            $user_list[] = array(
                'id' => $user_info['id'],
                'true_name' => $user_info['true_name'],
                'role_name' => $user_info['role_name'],
                'name' => $user_info['true_name'] . '(' . $user_info['role_name'] . ')'
            );
        }
        $this->success('获取业务经理和客户经理', $user_list);
    }

    public function get_invite_message()
    {
        $user_id = $this->getUserId();
        $user_info = UserModel::get($user_id);
        $query = OtherModel::where(['company_id' => $user_info['company_id']])->find();
        $res = unserialize($query['content'])['message'];
        $this->success('邀请注册', $res);
    }

    public function get_company_info()
    {
        $id = input('id');
        $company_info = CompanyModel::get($id);
        $this->success('获取公司信息', $company_info);
    }

    public function comress_image()
    {
        $list = AssetModel::where('more', '=', null)
            ->where('suffix', 'in', ['jpg', 'png', 'jpeg'])
            ->order('create_time desc')
            ->paginate(20, false, ['page' => 1]);
        $success = 0;
        $fail = 0;
        $percent = 0.8;
        foreach ($list as $key => $value) {
            $success += 1;
            $img_src = 'upload/' . $value['file_path'];
            if (file_exists($img_src)) {
                echo $img_src.$value['id'];
                $img = new Imgcompress($img_src, $percent);
                $img->compressImg($img_src);
                $fileImage = new File($img_src, 'r');
                AssetModel::where('id', $value['id'])->update(['file_size' => $fileImage->getSize(), 'more' => 1]);
            }
        }
        $this->success('成功' . $success . '失败' . $fail);
        $str = 'upload/default/20200508/8866a4e53f50d1a58f74a562bea9663a.jpg';
        $percent = 0.8;
        $dst_img = 'upload/default/20200508/test.png';
//        $upload = (new Imgcompress($str, $percent))->compressImg($str);
        $fileImage = new File($str, 'r');
        $this->success('', $fileImage->getSize());
    }
}
